import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';
import { NzModalRef, NzFormatEmitEvent, NzModalService, NzTreeComponent, NzTreeNodeOptions, NzMessageService } from 'ng-zorro-antd';
import { PagesService } from '../../services/pages.service';
import { UtilService } from '../../services/util.service';
import { RequestService } from '../../services/request.service';
declare var layui: any;
declare var layer: any;
declare var $: any;

@Component({
  selector: 'app-prodc',
  templateUrl: './prodc.component.html',
  styleUrls: ['./prodc.component.scss']
})
export class ProdcComponent implements OnInit {

  constructor(public router: Router, private http: HttpClient, public interfaceService: PagesService, public utilService: UtilService, public request: RequestService) {

  }
  field1 = []     //table表格1-主表列字段集合
  data1 = {}      //table表格1-主表对象数据集合
  data1_dis = {}  //table表格1-主表对象禁用集合
  data1_sel = []  //table表格1-主表下拉框数据集合
  data1_att = []  //table表格1-主表自定义属性数据集合
  componentName = 'app-prodc';
  @Output() private outer = new EventEmitter();
  @Input() puc_msg: any;

  /**
   * 表格字段动态列集合生成
   */
  filed_tablelist() {
    this.interfaceService.postPage('/erp/common/fields/getfieldsbytype', { 'type': 'prodOption.ProdClsOptionVo' }).subscribe((data: any) => {
      if (this.interfaceService.codeValidation(data)) {
      if (data.code == 200) {
        this.field1 = this.utilService.filed_list(data.object, 1);
        this.popup01_table();//初始化列表表格数据
      }
    }
    });
  }
  ngOnInit() {

    this.request.getProdCat1().subscribe((data) => {//商品树一级分类
      this.nodes = data;
      this.request.getBrand().subscribe((data) => {//品牌名称
        this.brandId = data.object.list;
        this.request.getColor().subscribe((data) => {//所有颜色
          this.colorName_list = data.object.list;
          this.request.getSpec().subscribe((data) => {//所有规格
            this.specId_list0 = data.object;
          });
        });
      });
    })
    this.filed_tablelist()

  }

  nodes = [];
  brandId = [];
  colorName_list = [];
  specId_list0 = [];

  //单独写：分类树、品牌、颜色、规格
  /*
  nodes = JSON.parse(localStorage.getItem('shu1'));
  brandId=JSON.parse(localStorage.getItem('pinpai'));
  colorName_list=JSON.parse(localStorage.getItem('yanse'));
  specId_list0=JSON.parse(localStorage.getItem('guige'));

   brandId;
  nodes = [];
  tree1(): void {//商品分类树一级
    var getProdCat1 = this.request.getProdCat1();
    getProdCat1.subscribe((data) => {           //商品分类
      this.nodes = data;
    })
    var getBrand = this.request.getBrand();			//品牌名称
    getBrand.subscribe((data) => {
      this.brandId = data.object.list;
    })
  } */
  ddata = [];
  tree2(e: Required<NzFormatEmitEvent>): void {
    if (e.node.getChildren().length === 0 && e.node.isExpanded) {
      if (e.node.origin.key) {
        this.loadNode('/erp/product/prodCat/opetion?prodCatId=' + e.node.origin.key).then((data: any) => {
          e.node.addChildren(data.object.list);
        });
      } else {
        e.node.addChildren(this.ddata);
      }
    }
  }

  xiaNodws = [];
  loadNode(xia): Promise<any[]> {
    this.interfaceService.getPages(xia, 1, 1000).subscribe((data: any) => {
      this.xiaNodws = data;
    });
    return new Promise(resolve => {
      setTimeout(() => resolve(this.xiaNodws),
        1000);
    });
  }

  //商品选择
  prodStatus_list = JSON.parse(localStorage.getItem('prodStatus_list'))	//商品状态
  popup01_url = '';
  popup01_id = '';
  popup01 = false;
  popup01_show(url): void {
    this.popup01_url = url
    this.popup01 = true;
    this.popup01_table();
  }
  popup01_hide(): void {
    this.popup01 = false;
    this.popup01_url = '';
    this.popup01_id = '';
    this.popup01_ss1 = '';
    this.popup01_ss2 = '';
    this.popup01_ss3 = '';
    this.popup01_ss4 = '';
    this.popup01_ss5 = '';
    this.popup01_ss6 = '';
  }

  popup01_ss1 = '';
  popup01_ss2 = '';
  popup01_ss3 = '';
  popup01_ss4 = '';
  popup01_ss5 = '';
  popup01_ss6 = '';

  indexTableId = 'popup01_tblist_prodc'
  popup01_table() {
    this.popup01_listTable(this.popup01_url + '&prodCode=' + this.popup01_ss1 + '&prodName=' + this.popup01_ss2 + '&prodCatId=' + this.popup01_ss3 + '&inputCode=' + this.popup01_ss4 + '&brandId=' + this.popup01_ss5 + '&prodStatus=' + this.popup01_ss6);
  }

  popup01_listTable(url): void {
    setTimeout(() => {
      layui.use(['table'], (obj) => {
        var  table = layui.table

        var tab = this.utilService.getTable(url, '#' + this.indexTableId, this.field1)
        tab['title'] = '数据表'
        tab['toolbar'] = '#toolbarIndex2_prodc'
        tab["done"] = (res) => {
          this.utilService.checkboxClick('nz-modal', this.indexTableId);
        }
        table.render(tab);
        //头工具栏事件
        table.on('toolbar(popup01_tblist_prodc)', (obj) => {
          var checkStatus = table.checkStatus(obj.config.id);
          switch (obj.event) {
            case 'getCheckData':
              var data = checkStatus.data;
              //layer.msg('选中了：'+ data.length + ' 个');
              if (data.length > 0) {
                this.popup01_hide();
                this.outer.emit(data);
              } else {
                layer.msg('请选择数据后操作！')
              }
              break;
          };
        });


      });

    }, 50);

  }


}
